var Http = {
	/**
	 * [HTTP GET 请求]
	 * @param [第1种使用方法是URL不带参数。第2种使用方法是在请求URL后带参数，如：?id=1&name=ming]
	 * 1. HTTP.get(url).then((data) => {}).catch((error) => {})
	 * 2. HTTP.get({url: url, data: [JSON Object] }).then((data) => {}).catch((error) => {})
	 * 
	 * [HTTP GET/POST请求实例]
	 * @param
	 * 1. const http1 = await this.$http.get(`https://b2.admin168.net/app/index.php?i=5&c=entry&a=wxapp&m=wjyk_clbg&do=qiniuYun`);
	 * 2. const http2 = await this.$http.get({url: 'https://b18.admin168.net/app/index.php?i=4&c=entry&a=wxapp&do=userCarList&m=wjyk_jscf',data: {uid: 2}});
	 */
	get: function(requestHandler) {
		if (typeof requestHandler === 'string') {
			requestHandler = {
				url: String(requestHandler),
				data: {}
			}
		}
		return this.Request('GET', requestHandler)
	},

	/**
	 * [HTTP POST 请求]
	 * @param [可自定义 headers，如需 Authorization 等，默认：'Content-Type': 'application/json']
	 * HTTP.post({url: url, data: [JSON Object], headers: [JSON Object] }).then((data) => {}).catch((error) => {})
	 */
	post: function(requestHandler) {
		return this.Request('POST', requestHandler)
	},

	/**
	 * [HTTP PATCH 请求]
	 * HTTP.patch({url: url, data: [JSON Object], headers: [JSON Object] }).then((data) => {}).catch((error) => {})
	 */
	patch: function(requestHandler) {
		return this.Request('PATCH', requestHandler)
	},

	/**
	 * [HTTP PUT 请求]
	 * HTTP.put({url: url, data: [JSON Object], headers: [JSON Object] }).then((data) => {}).catch((error) => {})
	 */
	put: function(requestHandler) {
		return this.Request('PUT', requestHandler)
	},

	/**
	 * [HTTP DELETE 请求]
	 * HTTP.delete({url: url, data: [JSON Object], headers: [JSON Object] }).then((data) => {}).catch((error) => {})
	 */
	delete: function(requestHandler) {
		return this.Request('DELETE', requestHandler)
	},

	// request
	Request: function(method, requestHandler) {
		const {
			url,
			data,
			headers,
			mask,
			mark,
			loading
		} = requestHandler

		if (loading === undefined || loading) {
			wx.showLoading && wx.showLoading({
				title: 'Loading...',
				mask: mask ? mask : false
			});
			mask ? wx.showNavigationBarLoading() : '';
		}
		
		return new Promise((resolve, reject) => {
			wx.request({
				url: url,
				data,
				method: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'].indexOf(method) > -1 ? method : 'GET',
				header: Object.assign({
					'Content-Type': 'application/json'
					/*
					这里可以自定义全局的头信息，这是一个栗子
					'Authorization': 'Bearer ' + wx.getStorageSync('token'),
					'X-Requested-With': 'XMLHttpRequest',
					'Content-Type': 'application/x-www-form-urlencoded'
					*/
				}, headers),
				success: function(res) {
					const {
						data,
						statusCode
					} = res
					// 处理数据
					statusCode === 200 ? resolve(data) : reject(data, statusCode)
					statusCode === 200 ? console.log(`mark is：%c ${mark?mark:'none'} %c`,
						`font-weight:bold;color:#18A05E;font-size:2em;`, `color:#18A05E;`) : console.log('');
				},
				fail: function() {
					reject('Network request failed')
					wx.showModal({
						title: '提示',
						content: `Network request failed`,
						showCancel: false,
						confirmText: '确认'
					});
				},
				complete: function() {
					wx.hideLoading && wx.hideLoading()
					wx.hideNavigationBarLoading() && wx.hideNavigationBarLoading()
				}
			})
		})
	}
}

const unparam = (str = '', unDecodeURI) => {
	let result = {},
		query = str.split('?')[1]

	if (!query) return result

	let arr = query.split('&')

	arr.forEach((item, idx) => {
		let param = item.split('='),
			name = param[0],
			value = param[1] || ''

		if (name) {
			result[name] = unDecodeURI ? value : decodeURIComponent(value)
		}
	})
	return result
}

module.exports = Http;
